import {
  _path,
  existplayer,
  bound,
  add_currency,
  _item,
  Write_cangku,
  Write_player
} from '../../mode/allapi'
import { AMessage, obtainingImages, plugin } from '../../api/api'

export class shop extends plugin {
  constructor() {
    super({
      dsc: '商店',
      rule: [
        {
          reg: /^(#|\/)?商店$/,
          fnc: 'shhangdian'
        },
        {
          reg: /^(#|\/)?购买(.*)*$/,
          fnc: 'shoping'
        },
        {
          reg: /^(#|\/)?集市.*换.*$/,
          fnc: 'market'
        }
      ]
    })
  }
  async shhangdian(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return
    const data = {
      MyData: await _item(id, 'shangdian')
    }
    const img = await obtainingImages(
      '/resources/html/shangdian/shangdian.html',
      data
    ).catch(err => {
      console.log(err)
      return '未知错误'
    })
    if (img) e.reply(img).catch(err => console.log(err))
    return true
  }
  async shoping(e: AMessage) {
    const log = []
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const data = await _item(id, 'shangdian')
    const player = await (await _path(id)).player
    const cangku = await (await _path(id)).cangku
    const didian = e.msg.replace(/(#|\/)?购买/, '')
    const irr = didian.split('*')
    const thing_data = irr[0]
    const thing_shu = parseInt(irr[1]) || 1
    const found = await bound(thing_data, data, 'id')
    if (!found) return false
    if (player.currency_num >= found.price * thing_shu) {
      await add_currency(-found.price * thing_shu, id)
      const foun = await bound(found.id, cangku, 'id')
      if (foun != undefined) {
        foun.num += Number(thing_shu)
      } else {
        found.num = thing_shu
        cangku.push(found)
      }
      log.push(
        `仓库：主公花费了${found.price * thing_shu}购买了${found.name}*${
          found.num
        }`
      )
    } else {
      log.push(
        `仓库：购买${found.name}*${found.num}需要${
          found.price * thing_shu
        }五铢钱`
      )
    }
    await Write_cangku('cangku', id, cangku)
    e.reply(log)
  }
  async market(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const player = await (await _path(id)).player
    const money = {
      石料: 'stone_num',
      木材: 'wood_num',
      铁矿: 'iron_num',
      粮草: 'food_num'
    }
    const match = e.msg.match(/\/集市(.*)换(.*)/)
    const result = match[1].split('*')
    if (!player[money[result[0]]]) return e.reply(`小贩:暂时无法换${result[0]}`)
    if (!money[match[2]]) return e.reply(`小贩:暂时无法换${match[3]}`)
    if (player[money[result[0]]] < result[1])
      return e.reply(`小贩:${result[0]}数量不足`)
    parseInt(
      (player[money[match[2]]] +=
        parseInt(result[1]) - parseInt(result[1]) * 0.3)
    )
    player[money[result[0]]] -= parseInt(result[1])
    await Write_player(id, player)
    e.reply(
      `小贩:置换完成获得${
        (match[2], parseInt(result[1]) - parseInt(result[1]) * 0.3)
      },欢迎下次再来`
    )
  }
}
